<?php
/**
 * 《电话记录》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../tel/Cdr.class.php');

class CdrMy extends Cdr {
	public $in_out = array(
		'-' => '请选择',
		'1' => '呼入',
		'2' => '呼出',		
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('in_out_option', $this->in_out);
	}
	
	function tab($controller, $request) {
		$data = array(
			'电话记录' => array('cc'=>'tel.php?do=CdrMy.find', 'js'=>'1'),

			// '拔打日志' => array('cc'=>'tel.php?do=CallMy.find', 'js'=>'1'),
			'正在通话' => array('cc'=>'tel.php?do=AsteriskMy.find', 'js'=>''),
		);
	
		$ret = $this->_tab($data);
	
		$controller->assign_by_ref('result', $ret);
	
		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$emp = $_SESSION['emp'];
		$exten = $emp['exten'];		
		if (empty($exten)) {
			$controller->display($request, 'tel/cdr/my/none');
			return true;
		}
		
		$controller->assign('t1', strtotime('-7 day'));

		$controller->display($request, 'tel/cdr/my/find');
		return true;
	}
	
	function _where_my($where, $in_out) {
		$emp = $_SESSION['emp'];
		$exten = $emp['exten'];	
		
		if (empty($where)) {
			if ($in_out == '1') {
				$my = sprintf("WHERE dst = '%s'", $exten);
			} else if ($in_out = '2') {
				$my = sprintf("WHERE src = '%s'", $exten);
			} else {
				$my = sprintf("WHERE src = '%s' OR dst = '%s'", $exten, $exten);
			}			
		} else {
			if ($in_out == '1') {
				$my = sprintf(" AND dst = '%s'", $exten);
			} else if ($in_out == '2') {
				$my = sprintf(" AND src = '%s'", $exten);
			} else {
				$my = sprintf(" AND (src = '%s' OR dst = '%s')", $exten, $exten);
			}
		}
		
		return $my;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);
		
		$cdr = 'cdr';
		$db = newdb($cdr);
		$fmt = "SELECT * FROM %s %s %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby("calldate");
		
		$my = $this->_where_my($where, $request['in_out_s']);
		$sql = sprintf($fmt, $this->tbl, $where, $my, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];

				$row = $this->_view($row['uniqueid'], $cdr);
				
				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'tel/cdr/my/list');
		return true;
	}
	
	function setting($controller, $request) {
		$emp = $_SESSION['emp'];
		$exten = $emp['exten'];
		
		if (empty($exten)) {
			$controller->display($request, 'tel/cdr/my/none');
			return true;
		}
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE exten = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_exten, $exten);
		debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);
		
		$controller->assign_by_ref('result', $row);
		
		$controller->display($request, 'tel/cdr/my/setting');
		return true;
	}
	
	function settingPost($controller, $request) {		
		$emp = $_SESSION['emp'];
		$exten = $emp['exten'];
		
		$cf = $request['cf'];
		$cfu = $request['cfu'];
		$cfb = $request['cfb'];
		$dnd = $request['dnd'];
		
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s WHERE exten = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_exten, $exten);
		// debug($sql);
		$result = $db->query($sql);
		$row = $db->fetch_array($result);
				
		$am = $this->am($row['exten']);
		
		$this->_setting_cf($am, $row, $cf);
		$this->_setting_cfu($am, $row, $cfu);
		$this->_setting_cfb($am, $row, $cfb);
		$this->_setting_dnd($am, $row, $dnd);

		$this->am_close($am);
				
		$fmt = "UPDATE %s SET cf = '%s', cfu = '%s', cfb = '%s', dnd = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, Constant::tbl_emp_exten, $cf, $cfu, $cfb, $dnd, $row['id']);
		// debug($sql);
		$db->query($sql);
	}
	
	function _setting_cf($am, $row, $val) {
		if (!$am) {
			return false;
		}
		
		if ($row['cf'] == $val) {
			return false;
		}

		if (empty($val)) {
			return $am->database_del('CF', $row['exten']);
		}

		$am->database_put('CF', $row['exten'], $val);
	}
	
	function _setting_cfb($am, $row, $val) {
		if (!$am) {
			return false;
		}
		
		if ($row['cfb'] == $val) {
			return false;
		}

		if (empty($val)) {
			return $am->database_del('CFB', $row['exten']);
		}

		$am->database_put('CFB', $row['exten'], $val);
	}
	
	function _setting_cfu($am, $row, $val) {
		if (!$am) {
			return false;
		}

		if ($row['cfu'] == $val) {
			return false;
		}

		if (empty($val)) {
			return $am->database_del('CFU', $row['exten']);
		}

		$am->database_put('CFU', $row['exten'], $val);
	}
	
	function _setting_dnd($am, $row, $val) {
		if (!$am) {
			return false;
		}

		if ($row['dnd'] == $val) {
			return false;
		}

		if (empty($val) || $val == 'NO') {
			return $am->database_del('DND', $row['exten']);
		}

		$am->database_put('DND', $row['exten'], $val);
	}
}
?>